/**
 * Decision Tree Classification With Uncertain Data (UDT)
 * Copyright (C) 2009, The Database Group,
 * Department of Computer Science, The University of Hong Kong
 *
 * This file is part of UDT.
 *
 * UDT is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * UDT is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.decisiontree.swing;

import com.decisiontree.datagen.DataGen;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import com.decisiontree.param.GlobalParam;
import com.decisiontree.param.SwingParam;
import javax.swing.JOptionPane;

/**
 * GenerateSelection
 * @author Smith Tsang
 * @since 0.9
 */
public class GenerateSelection extends javax.swing.JPanel {

    private static final String INVALID = "Invalid!";
    private static final String NOT_AVAILABLE = "Not Available!";

    private int noSamples = GlobalParam.DEFAULT_NO_SAMPLES;
    private double intervalWidth = GlobalParam.DEFAULT_WIDTH;
    private String type = DataGen.SAMPLE_GEN;
    private long seed = GlobalParam.DEFAULT_SEED;

    public int getNoSamples() {
        return noSamples;
    }

    public void setNoSamples(int noSamples) {
        this.noSamples = noSamples;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public double getIntervalWidth() {
        return intervalWidth;
    }

    public void setIntervalWidth(double intervalWidth) {
        this.intervalWidth = intervalWidth;
    }

        public long getSeed() {
        return seed;
    }

    public void setSeed(long seed) {
        this.seed = seed;
    }


    /** Creates new form GenerateSelection */
    public GenerateSelection() {
        initComponents();
        widthField.setText(intervalWidth +"");
        sampleField.setText(noSamples +"");
        seedField.setText(seed+"");
        initTypeBox();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        generateButton = new javax.swing.JButton();
        typeBox = new javax.swing.JComboBox();
        typeLabel = new javax.swing.JLabel();
        sampleField = new javax.swing.JTextField();
        sampleLabel = new javax.swing.JLabel();
        widthField = new javax.swing.JTextField();
        widthLabel = new javax.swing.JLabel();
        descriptionLabel = new javax.swing.JLabel();
        typeDetailLabel = new javax.swing.JLabel();
        sampleValidLabel = new javax.swing.JLabel();
        widthValidLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        seedLabel = new javax.swing.JLabel();
        seedField = new javax.swing.JTextField();
        seedValidLabel = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Generate Uncertain Data"));
        setMinimumSize(new java.awt.Dimension(400, 200));
        setPreferredSize(new java.awt.Dimension(400, 100));
        setLayout(new java.awt.GridBagLayout());

        generateButton.setText("Generate");
        generateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generateButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 0);
        add(generateButton, gridBagConstraints);

        typeBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                typeBoxActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 0);
        add(typeBox, gridBagConstraints);

        typeLabel.setText("Generate Type:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 11);
        add(typeLabel, gridBagConstraints);

        sampleField.setText("100");
        sampleField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                sampleFieldKeyTyped(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 0);
        add(sampleField, gridBagConstraints);

        sampleLabel.setText("Number of Samples:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 10);
        add(sampleLabel, gridBagConstraints);

        widthField.setText("0.1");
        widthField.setMaximumSize(new java.awt.Dimension(21, 21));
        widthField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                widthFieldKeyTyped(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 0);
        add(widthField, gridBagConstraints);

        widthLabel.setText("Interval Width (%):");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 10);
        add(widthLabel, gridBagConstraints);

        descriptionLabel.setText("Please select the following options for data generation.");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        add(descriptionLabel, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 3);
        add(typeDetailLabel, gridBagConstraints);

        sampleValidLabel.setFont(new java.awt.Font("Arial", 1, 12));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 3);
        add(sampleValidLabel, gridBagConstraints);

        widthValidLabel.setFont(new java.awt.Font("Arial", 1, 12));
        widthValidLabel.setFocusable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 3);
        add(widthValidLabel, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(jLabel1, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        add(jLabel2, gridBagConstraints);

        seedLabel.setText("Random Seed:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 10);
        add(seedLabel, gridBagConstraints);

        seedField.setText("0");
        seedField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                seedFieldKeyTyped(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 0);
        add(seedField, gridBagConstraints);

        seedValidLabel.setFont(new java.awt.Font("Arial", 1, 12));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 9, 3);
        add(seedValidLabel, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void setTypeDetailLabel(String type) {
        typeDetailLabel.setText(SwingParam.getDataGenDescription(type));
    }

    private void typeBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_typeBoxActionPerformed
        type = (String)typeBox.getSelectedItem();
        setTypeDetailLabel((String)typeBox.getSelectedItem());
        if(type.equals(DataGen.RANGE_GEN)){
            sampleField.setEnabled(false);
            seedField.setEnabled(false);
            sampleValidLabel.setText(NOT_AVAILABLE);
            seedValidLabel.setText(NOT_AVAILABLE);
        }else{
            sampleField.setEnabled(true);
            seedField.setEnabled(true);
            sampleValidLabel.setText("");
            seedValidLabel.setText("");
        }

    }//GEN-LAST:event_typeBoxActionPerformed

    private void widthFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_widthFieldKeyTyped
        try{
            intervalWidth = Double.parseDouble(widthField.getText());
            widthValidLabel.setText("");
        }catch(NumberFormatException e){
            widthValidLabel.setText(SwingParam.INVALID);
        }

    }//GEN-LAST:event_widthFieldKeyTyped

    private void sampleFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_sampleFieldKeyTyped
         try{
            noSamples = Integer.parseInt(sampleField.getText());
            sampleValidLabel.setText("");
        }catch(NumberFormatException e){
            sampleValidLabel.setText(SwingParam.INVALID);
        }
    }//GEN-LAST:event_sampleFieldKeyTyped

    private void generateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generateButtonActionPerformed
        if(widthValidLabel.getText().equals(SwingParam.INVALID) || seedValidLabel.getText().equals(SwingParam.INVALID)
                || sampleValidLabel.getText().equals(SwingParam.INVALID))
            JOptionPane.showMessageDialog(this, "Please Enter valid values", "Warning", JOptionPane.WARNING_MESSAGE);
        
    }//GEN-LAST:event_generateButtonActionPerformed

    private void seedFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_seedFieldKeyTyped
        try{
            seed = Long.parseLong(seedField.getText());
            seedValidLabel.setText("");
        }catch(NumberFormatException e){
            seedValidLabel.setText(INVALID);
        }
    }//GEN-LAST:event_seedFieldKeyTyped



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel descriptionLabel;
    private javax.swing.JButton generateButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField sampleField;
    private javax.swing.JLabel sampleLabel;
    private javax.swing.JLabel sampleValidLabel;
    private javax.swing.JTextField seedField;
    private javax.swing.JLabel seedLabel;
    private javax.swing.JLabel seedValidLabel;
    private javax.swing.JComboBox typeBox;
    private javax.swing.JLabel typeDetailLabel;
    private javax.swing.JLabel typeLabel;
    private javax.swing.JTextField widthField;
    private javax.swing.JLabel widthLabel;
    private javax.swing.JLabel widthValidLabel;
    // End of variables declaration//GEN-END:variables


    public void initTypeBox(){
//        typeBox.getModel().setSelectedItem("RANGE");
//        typeBox.getModel().setSelectedItem("SAMPLE");
        typeBox.addItem(DataGen.RANGE_GEN);
        typeBox.addItem(DataGen.SAMPLE_GEN);
        typeBox.setSelectedItem(DataGen.SAMPLE_GEN);
        setTypeDetailLabel(DataGen.SAMPLE_GEN);
    }

    public static void main(String args[]){
        JFrame frame = new JFrame("Generate Uncertain Data");
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        }

        frame.add(new GenerateSelection());
        frame.setSize(500,300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        frame.setPreferredSize(new Dimension(300,200));
        frame.setVisible(true);


    }


}
